package nl.windesheim.archdemo.presentationmodel;


import nl.windesheim.archdemo.dao.OrderDao;
import nl.windesheim.archdemo.model.Order;
import nl.windesheim.archdemo.model.OrderLine;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import nl.windesheim.archdemo.dao.GlobalInstances;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

/**
 *
 * @author re0053273
 */
public class OrderLineTablePresenter implements TableModel {

    OrderDao orderDao = GlobalInstances.getOrderDao();
    /** Order we are currently showing details for */
    private Order order;
    /** Only use through getter because of lazy loading */
    private List<OrderLine> orderLines;
    private List<TableModelListener> listeners;

    public void setOrder(Order o){
        this.order = o;
        this.orderLines = null;
        this.fireChangeEvent();
    }
    
    private void fireChangeEvent() {
        if (listeners != null) {
            for (TableModelListener cml : listeners) {
                cml.tableChanged(new TableModelEvent(this));
            }
        }
    }
        
    public void refresh(){
        this.orderLines = this.order.getOrderLines();
        this.fireChangeEvent();
    }
    
    /** If an order is set, retrieve its orderlines
     * if not: returns an empty list.
     * FIXME: doesnt make sense to do this lazily if hibernate loads it eagerly
     */
    private List<OrderLine> getOrderLines(){
        if( this.order == null ){
            return Collections.emptyList();
        }
        if( this.orderLines == null ){
            this.orderLines = this.order.getOrderLines();
        } 
        return this.orderLines;
    }
    
    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public String getColumnName(int i) {
        switch(i){
            case 0: return "Product";
            case 1: return "Aantal";
            case 2: return "TotaalPrijs";
            default: throw new ArrayIndexOutOfBoundsException("OrderLine table has only 3 cols");
        } 
    }

    @Override
    public int getRowCount() {
        if( this.getOrderLines() == null ){
            return 0;
        }
        return this.getOrderLines().size();
    }
    
    @Override
    public Object getValueAt(int row, int col) {
        OrderLine o = this.getOrderLines().get(row);
        Object value = null;
        switch(col){
            case 0: 
                value = o.getProduct().getName();
                break;
            case 1: 
                value = o.getAmount();
                break;
            case 2: 
                value = o.getProduct().getPrice().multiply(new BigDecimal(o.getAmount()));
                break;
            default: throw new ArrayIndexOutOfBoundsException("Order table has only 4 cols");
        } 
        if( value == null ){
            return "";
        }
        return value.toString();
    }

    @Override
    public Class<?> getColumnClass(int i) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int i, int i1) {
        return false;
    }
    
    @Override
    public void setValueAt(Object o, int i, int i1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void addTableModelListener(TableModelListener tl) {
        if( listeners == null ){
            listeners = new ArrayList<TableModelListener>(1);
            listeners.add(tl);
        }
    }    
    @Override
    public void removeTableModelListener(TableModelListener tl) {
        if(listeners != null){
            listeners.remove(tl);
        }
    }

}


